#include "<SampleEd$Dir>.h.Driver"
#include "osfile.h"
#include "kernel.h"
#include "sndfile.h"

/* Export General file */

osbool driver_export(char *filename,
                     int export_index,
                     int filetype,
                     int samplerate,
                     int channels,
                     int start,
                     int size)
    {
    int channelindex,     /* Channel counter       */
        data[32],         /* Output data           */
        error,            /* Error number          */
        index,            /* Index into sample     */
        lastpc = 0,       /* Previous percentge    */
        pc,               /* Calculated percentage */
        left,
        right,
        real_channels;

    DriverFileType *filetypes;

    _kernel_swi_regs r;

    FILE *fh;

    SF_INFO wfinfo;       /* File format info      */

    SNDFILE *sndfile;     /* libsndfile handle     */

    filetypes = driver_filetypes();

    if (filetype == 0x0cd && (samplerate != 44100 || channels != 2))
        {
        driver_report_error("DrvCDForm");
        return TRUE;
        }

    if ((fh = fopen(filename,"w")) == NULL)
        {
        driver_report_error("DrvErrOpenW");
        return TRUE;
        }

    fclose(fh);

/* Get real channels */

    driver_get_sample_details(NULL,NULL,&real_channels,NULL);

/* Open file */

    wfinfo.samplerate = samplerate;
    wfinfo.channels   = channels;

    wfinfo.format = export_index;

    sndfile = sf_open(filename,SFM_WRITE,&wfinfo);

/* Report an error */

    error = sf_error(sndfile);

    if (error)
        {
        driver_report_error((char *)sf_error_number(error));
        }

/* Read data */

    if (sndfile != NULL)
        {
/* Loop round data */

        for (index = start;
             index < size;
             index++)
            {
/* Calculate percentage */

            pc = 100 * (index - start) / (size - start);

/* Only set percentage if different */

            if (pc > lastpc)
                {
                driver_percentage(pc);
                lastpc = pc;
                }

/* Loop round each channel */

            if (channels == real_channels)
                {
                for (channelindex = 0;
                     channelindex < channels;
                     channelindex++)
                    {
                    data[channelindex] = driver_get_sample(index,channelindex) << 16;
                    }
                }
            else
                {
                driver_mix(index,channels,&left,&right);

                if (channels == 1)
                    {
                    data[0] = left << 16;
                    }
                else
                    {
                    data[0] = left << 16;
                    data[1] = right << 16;
                    }
                }

/* Write input */

            sf_write_int(sndfile,data,channels);

/* Check for error */

            error = sf_error(sndfile);

            if (error)
                {
                driver_report_error((char *)sf_error_number(error));
                break;
                }
            }

        sf_close(sndfile);
        }

    r.r[0] = OSFile_SetType;
    r.r[1] = (int)filename;
    r.r[2] = (int)filetype;

    _kernel_swi(XOS_File,&r,&r);

    return FALSE;
    }
